package com.woshua.vpimage.adapter

import android.content.Context
import android.graphics.Bitmap
import android.support.v4.view.PagerAdapter
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.Transformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.FitCenter
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.woshua.vpimage.R

/**
 * @author Steven.zhao
 * email:hongtu.zhao@goodwinsoft.net
 * date:2019/7/29
 * desc:
 */
class MyPageAdapter:PagerAdapter() {

    private val mImages= arrayOf(R.mipmap.meinv_three,
            R.mipmap.meinv_one,R.mipmap.meinv_two,
            R.mipmap.meinv_three,R.mipmap.meinv_one)

    override fun isViewFromObject(view: View, obj: Any): Boolean=view==obj
    override fun getCount(): Int =mImages.size

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val imageView=ImageView(container.context)
        imageView.scaleType=ImageView.ScaleType.FIT_XY
        imageView.layoutParams=ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT)
//        imageView.setImageResource(mImages[position])
        centerCropAndRoundCornerImg(container.context,mImages[position],imageView)
        container.addView(imageView)
        return imageView
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View?)
    }

    private val options=RequestOptions().transform(MultiTransformation(FitCenter(), RoundedCorners(20)))
    //居中裁剪+圆角
    private fun centerCropAndRoundCornerImg(context:Context,resource:Int,imageView: ImageView) {
        Glide.with(context)
                .load(resource)
                .apply(options)
                .into(imageView)
    }
}